/**
 * 这是未拆分时的配置文件
 */

var path = require("path");
var HtmlWebpackPlugin = require("html-webpack-plugin");
var BundleAnalyzerPlugin = require("webpack-bundle-analyzer").BundleAnalyzerPlugin;
const webpack = require("webpack");
const { CleanWebpackPlugin } = require("clean-webpack-plugin");
const { compilation } = require("webpack");
const { callbackify } = require("util");

class IBasePlugin {
  constructor(options) { }
  
  apply(compiler) {
    const hooks = compiler.hooks;
    if(hooks) {
      hooks.emit.tapAsync('IBasePlugin',(compilation, callback=()=>{})=>{
        compilation.chunks.forEach(function(chunk){
          chunk.files.forEach(function (filename) {
            // compilation.assets 存放当前所有即将输出的资源
            // 调用一个输出资源的 source() 方法能获取到输出资源的内容
            console.log("filename:", filename)
            let source = compilation.assets[filename].source();
          });
        })

        // 这是一个异步事件，要记得调用 callback 通知 Webpack 本次事件监听处理结束。
      // 如果忘记了调用 callback，Webpack 将一直卡在这里而不会往后执行。
        callback();
      })
    } else {


    }
  }
}
debugger;
module.exports = {
  entry: path.resolve(__dirname, "./demo"),
  output: {
    path: path.resolve(__dirname, "./dist"),
    filename: '[name][chunkhash:5].bundle.js',
    chunkFilename: '[name].[chunkhash:5].js',
    publicPath: "/",// dev? "http://localhost:8080/" : "https://cdn/" 可以用来指定输出文件的访问路径/path。 Demo: Dev: "./test.png", prod: "https://cdn/test.png"
  },
  devtool: "cheap-module-source-map", //"cheap-module-eval-source-map", // "nosources-source-map", //"inline-source-map",
  devServer: {
    open: true,
    port: 8666,
    hot: true,
    contentBase: path.resolve(__dirname, "./dist")
  },
  plugins: [
    new webpack.DllReferencePlugin({
      manifest: path.resolve(__dirname, 'dist', 'dll', 'manifest.json')
    }),
    new CleanWebpackPlugin({
      cleanOnceBeforeBuildPatterns: ['**/*', '!dll', '!dll/**'] // 不删除dll目录
    }),
    new webpack.HotModuleReplacementPlugin,
    new HtmlWebpackPlugin({
      title: "iWebPack Demo",
      template: path.resolve(__dirname, "./template.html"),
      inject: true,
    }),
    //new BundleAnalyzerPlugin(), // 编译、启动完成后打开加载资源的分析界面
    new webpack.ContextReplacementPlugin(
      /moment[/\\]locale$/,
      /zh-cn|en-us/,
    ),
    new IBasePlugin(),
  ],
  mode: 'development', // production  切换mode 会开启压缩功能
  module: {
    rules: [
      {
        test: /\.js|jsx|ts|ts(x?)$/,
        exclude: /node_modules/,
        use: {
          loader: 'babel-loader',
          options: {
            presets: [
              "@babel/preset-env",
              "@babel/preset-react",
              "@babel/preset-typescript"
            ]
          }
        }
      },
      {
        test: /\.css|scss$/,
        exclude: /mode_modules/,
        use: [
          "style-loader", 'css-loader', 'sass-loader'
        ]
      }, {
        test: /\.(png|jpg|gif)$/,
        use: [
          {
            loader: 'file-loader',
            options: {
              name: './imgs/[name].[ext]'
            }
          }
        ]
      }
    ]
  },
  optimization: {
    runtimeChunk: {
      name: 'manifest'
    },
    splitChunks: {
      cacheGroups: {
        vendors: {
          test: /[\\/]node_modules[\\/]/,
          name: 'vendors',
          chunks: "all",
          // minChunks: 5,  加此行为何 无法提取vendors文件 ？？？？？？  因为minChunks 表示 拆分前必须共享模块的最小块数
          priority: 10,
        }/*,
        commons:{
          name: 'commons',
          chunks: "all",
          minSize:1,
          minChunks:1,
          priority: 1
        }*/
      }
    }
  },
  /**支持webpack, 项目文件内不写扩展名 */
  resolve: {
    extensions: ['.js', '.css', '.scss', '.ts', '.tsx', '.jsx','.png']
  }
}